<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SGoodsGroup extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询拼团商品数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalGoodsGroupList($where, $field,$limit,$page){
        $order = [
            "a.goods_group_id" =>"desc",
        ];
        $result = $this->alias('a')->join('s_goods b', 'a.goods_id = b.goods_id')->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 更改商品状态
     * @param $userId
     * @param $status
     * @return bool
     */
    public function changeStatusGoodsModel($goodsGroupId,$goodsStatus){
        if(empty($goodsGroupId)){
            return false;
        }
        $where = [
            ['goods_group_id', '=', $goodsGroupId]
        ];
        if($goodsStatus == 1){
            $data = [
                'goods_status'=>$goodsStatus,
                'action_status'=> '1',
            ];
        }else{
            $data = [
                'goods_status'=>$goodsStatus,
                'action_status'=> '0',
            ];
        }
        $result = $this->where($where)->save($data);
        return $result;
    }

    /**
     * 根据$id获取拼团商品数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsGroupByIdFind($id,$field = "*"){
        if(empty($id)){
            return false;
        }
        $where = [
            "goods_group_id" =>$id,
        ];
        $result = $this->where($where)->field($field)->find();
        return $result;
    }

    public function DelGoodsGroupIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['goods_group_id', 'in', $ids],
            ['del_flag', '=', '0']
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function getNormalGoodsGroupCount($where){
        $count = $this->alias('a')->where($where)->count();
        return $count;
    }

    public function getGoodsGroupByGoodsIdModel($goods_id,$goods_group_id){
        if(empty($goods_id)){
            return false;
        }
        $where = [
            "goods_id" =>$goods_id,
            "del_flag"=>'0',
        ];
        if($goods_group_id){
            $result = $this->where("goods_group_id", "<>", $goods_group_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }
}
